"""
@author: 江同学呀
@file: token_count_field.py
@date: 2024/7/28 1:07
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/token-count.html

    A count of tokens in a text.
    文本中的标记计数。
"""
from abc import ABC
from typing import Type, Union, Dict, Any

from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_string_field import _BaseStringField
from espc.orm.model.mapping.field.field_attribute import (
    IndexAttribute, StoreAttribute, AnalyzerAttribute,
    EnablePositionIncrementsAttribute, BoostAttribute, DocValuesAttribute, NullValueAttribute
)
from espc.utils.build_add_attr import build_add_attr


class TokenCountField(
    _BaseStringField, AnalyzerAttribute, EnablePositionIncrementsAttribute, BoostAttribute, DocValuesAttribute,
    IndexAttribute, NullValueAttribute, StoreAttribute, ABC
):
    """
    令牌计数 字段
    """
    _field_type: str = "token_count"

    def __init__(
            self, field_name: str = None, value: str = None, default: str = None, analyzer: str = None,
            enable_position_increments: bool = None, boost: float = None, doc_values: bool = None,
            null_value: Any = None, index: bool = None, store: bool = None
    ):
        super(TokenCountField, self).__init__(
            field_name=field_name, value=value, default=default, analyzer=analyzer,
            enable_position_increments=enable_position_increments, boost=boost, doc_values=doc_values,
            null_value=null_value, index=index, store=store,
        )
        return

    def __get__(self, instance: _Base, owner: Type["TokenCountField"]) -> Union["TokenCountField", str]:
        return super(TokenCountField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: str):
        super(TokenCountField, self).__set__(instance=instance, value=value)
        return

    def __getattribute__(self, item):
        return super(TokenCountField, self).__getattribute__(item)

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping



